package com.example.demo6.ui.center;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.ui.lose.LoseViewModel;
import com.example.demo6.ui.lose.LostAdapter;

import java.util.ArrayList;
import java.util.List;

public class myloseFragment extends Fragment {
    private List<LostGoods> lostGoodsList;
    RecyclerView recyclerView;
    MylostAdapter mylostAdapter;
    private MyloseViewModel mViewModel;

    public static myloseFragment newInstance() {
        return new myloseFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_mylose, container, false);
        recyclerView=view.findViewById(R.id.mylostrecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        lostGoodsList=new ArrayList<>();
        mylostAdapter=new MylostAdapter(getContext(),lostGoodsList);
        recyclerView.setAdapter(mylostAdapter);
        MyloseViewModel recyclerViewViewModel = new ViewModelProvider(this).get(MyloseViewModel.class);
        recyclerViewViewModel.getlistLiveData().observe(getViewLifecycleOwner(), new Observer<List<LostGoods>>() {
            @Override
            public void onChanged(List<LostGoods> lostGoods) {
                lostGoodsList.clear();
                lostGoodsList.addAll(lostGoods);
                mylostAdapter.notifyDataSetChanged();
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(MyloseViewModel.class);
        // TODO: Use the ViewModel
    }

}